package com.trkj.usercenter.charts;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.trkj.base.Constants;
import com.trkj.base.OnResponseResultListener;
import com.trkj.base.newwork.ProgressBarDataFromRemote;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图表统计：成绩等级分布
 * Created by LiZanhong on 2015/06/25.
 */
public class ScoreLevelFragment extends Fragment{
    private View root;
    private ChartBarsUtils chartBarUtils;
    private String[] flags = {"a", "b", "c", "d", "e"};

    public String getUrl(){
        return MessageFormat.format(Constants.ApiUrl.CHART_SCORE_LEVEL,
                UserService.getInstance(this.getActivity()).parseUser().userId
                );
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        chartBarUtils = new ChartBarsUtils();
        chartBarUtils.setTitle("成绩等级分布（单位：次）");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(root == null){
            root = inflater.inflate(R.layout.fragment_chart_bar, null);
            final BarChart barChart = (BarChart) root.findViewById(R.id.chart);

            ProgressBarDataFromRemote dataFromRemote =
                    new ProgressBarDataFromRemote(getActivity());
            dataFromRemote.setResponseResultListener(new OnResponseResultListener() {
                @Override
                public void onResponseResult(String result) {
                    JSONObject jsonObject = JSON.parseObject(result);
                    if(jsonObject.getBoolean("success")){
                        JSONArray data = jsonObject.getJSONArray("data");
                        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                        for(int i = 0; i < flags.length; i ++){
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("label", getLevel(flags[i]));
                            map.put("value", data.getJSONObject(0).getInteger(flags[i]));
                            list.add(map);
                        }

                        BarData barData = chartBarUtils.getBarData(list);
                        chartBarUtils.showChart(barChart, barData);
                    }
                }
            });

            dataFromRemote.execute(getUrl());
        }

        return root;
    }

    private String getLevel(String flag){
        String[] levels = {"优秀", "良好", "中等", "及格", "不及格"};


        for(int i = 0; i < flags.length; i ++){
            if(flag.equals(flags[i])) {
                return levels[i];
            }
        }
        return flags[flags.length - 1];
    }
}
